define(['app'], function (app) {
    app.controller('joinCtrl', function ($scope, $http, $state, $interval) {
        //获取班级类型
        $http({
            url: '/OSRS/evallist/clazztype',
            method: 'post'
        }).then(function (res) {
            $scope.clazz = res.data

        })
        //获取教员姓名
        $http({
            url: '/OSRS/evallist/user',
            method: 'post'
        }).then(function (res) {
            $scope.user = res.data
        })
        //获取所有的模板
        $http({
            url: '/OSRS/evallist/template',
            method: 'post'
        }).then(function (res) {
            $scope.template = res.data
        });

        function refCount() {

            $http({
                method: 'GET',
                url: '/OSRS/evallist/count'
            }).then(function (res) {
                console.log(res.data);
                $scope.countMap = res.data;
                // for( var i=0;i<$scope.data.length;i++){
                //     $scope.data[i].docount=res.data[$scope.data[i].researchid][0];
                //     $scope.data[i].finshcount=res.data[$scope.data[i].researchid][1];
                // }
                // var doInputs=document.getElementsByName("doCount");
                // for (var i = 0; i < doInputs.length; i++) {
                //     doInputs[i].value=res.data[doInputs[i].placeholder][0];
                // }
            });
        };
        init2();
        // var timer = $interval(function () {
        //     refCount();
        //     // console.log("计时器关闭");
        // }, 3000);

        var timer = setInterval(function () {
            refCount();
            // console.log("计时器关闭");
        }, 3000);
        //在控制器里，添加$on函数
        $scope.$on('$destroy', function () {
           // $interval.cancel(timer);
            window.clearInterval(timer);
            console.log("计时器关闭");
        });

        // timer.then(function(){ console.log('创建成功')},
        //     function(){ console.log('创建不成功')});
        function init2() {

            $http({
                url: '/OSRS/evallist/checklist?pageNow=1&pageSize=5',
                method: 'get'
            }).then(function (res) {
                console.log(res.data);
                $scope.data = res.data.pageDatas;
                $scope.totalRecord = res.data.totalRecords;
                $scope.pageNow = res.data.pageNow;
                $scope.pageSize = res.data.pageSize;
                refCount();
                layui.use(['laypage'], function () {
                    var laypage = layui.laypage;

                    //执行一个laypage实例
                    laypage.render({
                        elem: 'pagination', //注意，这里的 test1 是 ID，不用加 # 号
                        count: $scope.totalRecord,//页总数，从服务端得到
                        curr: $scope.pageNow,
                        limit: $scope.pageSize,
                        jump: function (obj, first) {
                            //首次不执行
                            if (!first) {
                                //obj包含了当前分页的所有参数，比如：


                                $http({
                                    method: 'GET',
                                    url: '/OSRS/evallist/checklist?pageNow=' + obj.curr + '&pageSize=' + obj.limit
                                }).then(function (res) {
                                    $scope.data = res.data.pageDatas;
                                    $scope.totalRecord = res.data.totalRecords;
                                    $scope.pageNow = res.data.pageNow;
                                    $scope.pageSize = res.data.pageSize;
                                });

                            }
                        }
                    });


                })
            })

        }


        // //增加调查
        // $scope.add=function (clazzid,userid,tempid) {
        //     alert(clazzid)
        //     alert(userid)
        //     alert(tempid)
        //     $http({
        //
        //         url:'/OSRS/evallist/add',
        //         method:'post',
        //         data:clazzid+";"+userid+";"+tempid
        //     }).then(function (res) {
        //         layer.msg("添加成功");
        //         init2();
        //
        //     })
        // }
        //结束调查
        $scope.end = function (y) {
            $http({
                url: "/OSRS/evallist/end?Researchid=" + y,
                method: "post"
            }).then(function (res) {

                init2();

            }, function (err) {

            })

        }
        //删除调查
        $scope.delete = function (x) {
            $http({
                url: "/OSRS/evallist/delete?Researchid=" + x,
                method: "get"
            }).then(function (res) {

                layer.msg("删除成功");
                init2();
            }, function (err) {
                layer.msg("服务器未响应");
                console.log(err)
            })
        }

        //复制调查路径
        $scope.review = function (x) {
            function getRootPath() {
                //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
                var curWwwPath = window.document.location.href;
                //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
                var pathName = window.document.location.pathname;
                var pos = curWwwPath.indexOf(pathName);
                //获取主机地址，如： http://localhost:8083
                var localhostPaht = curWwwPath.substring(0, pos);
                //获取带"/"的项目名，如：/uimcardprj
                var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
                return (localhostPaht + projectName);
            }

            var hideInput = document.getElementById("biao1");
            hideInput.value = getRootPath() + "/prv/research?id=" + x;
            hideInput.select();

            document.execCommand("Copy");
            layer.msg("复制完毕,路径:" + hideInput.value);
        }
        $('body').scrollTop(0);
        layui.use(['form', 'laypage'], function () {
            var form = layui.form;
            var laypage = layui.laypage;
            form.render();
            //监听提交
            form.on('submit(formDemo)', function (data) {
                $http({

                    url: '/OSRS/evallist/add',
                    method: 'post',
                    data: JSON.stringify(data.field)
                }).then(function (res) {
                    layer.msg("添加成功");
                    init2();
                })
                return false;
            });
            // //执行一个laypage实例
            // laypage.render({
            //     elem: 'pagination', //注意，这里的 test1 是 ID，不用加 # 号
            //     count: 50 //数据总数，从服务端得到
            // });
        });

    });
});